package handler

import (
	"github.com/gin-gonic/gin"
	"github.com/google/uuid"
	"log"
	"net/http"
	"path/filepath"
	"strconv"
	"yk/api/request"
	"yk/internal/dao"
	"yk/internal/model"
	"yk/pkg"
)

func ListVideo(c *gin.Context) {
	var list []model.Video

	dao.ListVideo(&list)

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "展示成功",
		"data": list,
	})
}

func DetailVideo(c *gin.Context) {
	atoi, _ := strconv.Atoi(c.Query("id"))
	id := uint(atoi)

	var video model.Video
	dao.GetOneById(id, &video)

	if id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "未查询到数据",
			"data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": video,
	})
}

func UpdateVideo(c *gin.Context) {
	var req request.UpdateVideo
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "参数验证",
			"data": err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "修改成功",
		"data": nil,
	})
}

func DelVideo(c *gin.Context) {
	var req request.DelVideo
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "参数验证",
			"data": err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "删除成功",
		"data": dao.Delete(&model.Video{
			Id: req.Id,
		}),
	})

}

func Upload(c *gin.Context) {
	file, _ := c.FormFile("file")
	log.Println(file.Filename)

	dst := "./static/upload." + file.Filename
	// 上传文件至指定的完整文件路径
	filename := uuid.NewString() + filepath.Ext(file.Filename)
	c.SaveUploadedFile(file, dst)

	pkg.Upload(dst, filename)

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "上传成功",
		"data": dst,
	})
}
